<template>
  <div class="p-4">
    <!--    头部的内容-->
    <div class="flex items-center pb-4">
      <slot name="header">
        <div class="text-20px leading-20px">{{ title }}</div>
        <div class="flex-1"></div>
        <div>
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <!--    列的内容-->
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        v-if="showColumnIndex"
        prop="index"
        label="序号"
        width="80"
        type="index"
        align="center"
      ></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!--    尾部内容-->
    <div class="flex justify-end" v-if="isShowPagination">
      <slot name="footer">
        <el-pagination
          :currentPage="pagenationInfo.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pagenationInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'

const emit = defineEmits(['selectionChange', 'update:paginationInfo'])
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  listData: {
    type: Array,
    required: true
  },
  listCount: {
    type: Number,
    default: 0
  },
  propList: {
    type: Array,
    required: true
  },
  showColumnIndex: {
    type: Boolean,
    default: false
  },
  showSelectColumn: {
    type: Boolean,
    default: false
  },
  pagenationInfo: {
    type: Object,
    default: () => ({ currentPage: 0, pageSize: 10 })
  },
  childrenProps: {
    type: Object,
    default: () => ({})
  },
  isShowPagination: {
    type: Boolean,
    default: true
  }
})

const handleSelectionChange = (value: any) => {
  emit('selectionChange', value)
}
const handleSizeChange = (pageSize: number) => {
  emit('update:paginationInfo', { ...props.pagenationInfo, pageSize })
}
const handleCurrentChange = (currentPage: number) => {
  emit('update:paginationInfo', { ...props.pagenationInfo, currentPage })
}
</script>

<style scoped></style>
